home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / book / applets / zine / zine.jav < prev    next >
Encoding:
Text File  |  1995-10-16  |  8.0 KB  |  409 lines

  1. /*
  2.  *Zine Zine Zine Zine Zine Zine Zine
  3.  * a demo program.
  4.  * Implements popups for comic book style messages
  5.  * on top of an image. 
  6.  * uses a crummy form of markup language
  7.  * designed only to be easy to parse with java
  8.  * (read quick n dirty)
  9.  *Zine (more...)
  10.   * an app by Johan van der Hoeven.
  11.   * johan@rosebud.com
  12.   * http://www.rosebud.com/rb/rbhome.html
  13.   * johan_van_der_Hoeven@terc.edu
  14.   * 
  15.   * can be viewed at:
  16.   * http://http://www.fanzine.se/java/jo/zine.html (zine stuff)
  17.   * http://teaparty.terc.edu/java/z/glob.html (network science education research)
  18. */
  19.  
  20.  
  21.  
  22.  
  23. import java.awt.*;
  24. import java.io.StreamTokenizer;
  25. import java.io.InputStream;
  26. import java.io.FileInputStream;
  27. import java.io.IOException;
  28. import java.net.URL;  
  29. import java.net.MalformedURLException;
  30. import java.util.Vector;
  31. import java.util.Enumeration; 
  32.  
  33. class Rect
  34. {
  35.  
  36. public int lX=0;
  37. public int tY=0;
  38. public int rX=0;
  39. public int bY=0;
  40. public Rect(int leftX, int topY, int rightX, int bottomY)
  41.     {lX=leftX; tY=topY; rX=rightX; bY=bottomY;}
  42.  
  43. public Rect(){};
  44. public boolean isPointInRect(int x, int y)
  45.     {
  46.     if(x<rX&&x>lX&&y>tY&&    y<bY)
  47.         return true;
  48.     else
  49.         return false;
  50.  
  51.     }
  52. public void offset(int dx, int dy)
  53.     {
  54.     lX+=dx;
  55.     rX+=dx;
  56.     tY+=dy;
  57.     bY+=dy;
  58.     };
  59. public int width()     { return (rX-lX);};
  60. public int height()     { return (bY-tY);};
  61.  
  62. class zineFrame
  63. {
  64. public   Point p;
  65. public    Rect r;
  66. public    Vector strvec;    
  67.     /*  add more later...type etc.*/
  68.    
  69. public zineFrame()
  70.     { r = new Rect(); strvec = new Vector(); p =new Point(0, 0);
  71.     };
  72.    
  73. }  
  74.   
  75. /* you may wonder why there is a clipR when it is the same as textR*/
  76. /* this is so that when the popup gets better looking the texrR */
  77. /* will be a part of it */    
  78. class zinePopUp
  79. {
  80. public Point p;   /* anchor */
  81. public Rect clipR; /* area it covers */
  82. public Rect textR; /*area for text*/
  83. public zineFrame xfrm=null;
  84. public Font font;
  85. public FontMetrics fm;
  86. protected int lineHi, charWid;
  87.    
  88.  
  89. public zinePopUp    (Font f, FontMetrics fm) {
  90.     p= new Point(0, 0); clipR = new Rect(); textR= new Rect();
  91.     //font = new java.awt.Font("TimesRoman", Font.PLAIN, 12);
  92.     this.fm = fm;
  93.     font = f;
  94.     charWid=fm.stringWidth(" ");
  95.     lineHi=fm.getHeight()+2;
  96. };
  97. public boolean doLayout(Rect boundR, zineFrame zf)
  98.     {
  99.            
  100.     xfrm=zf;
  101.     int wid=0;
  102.     int hi=0;
  103.             
  104.     for (Enumeration e = xfrm.strvec.elements(); e.hasMoreElements();)
  105.         {
  106.         hi+=lineHi;
  107.  
  108.         String tmp=    (String) e.nextElement();
  109.  
  110.         wid= Math.max(wid, fm.stringWidth(tmp));
  111.                     
  112.                     
  113.         }; 
  114.     wid+=2*charWid;
  115.     textR.bY=xfrm.p.y;
  116.     textR.lX=xfrm.p.x;
  117.     textR.rX=textR.lX+wid;
  118.     textR.tY=textR.bY-hi-lineHi/2;
  119.     doRectMunge(boundR);
  120.     clipR=textR;
  121.  
  122.     return true;
  123.     };
  124. protected void doRectMunge(Rect boundR)
  125.     {
  126.     int xa=xfrm.p.x-boundR.lX;
  127.     int xb=boundR.rX-xfrm.p.x;
  128.     int ya=xfrm.p.y-boundR.tY;
  129.     int yb=boundR.bY-xfrm.p.y;
  130.  
  131.     /* check if it fit, if not try moving it. */
  132.         
  133.     if(textR.rX>boundR.rX)
  134.         {  
  135.                 //do a horizontal left shift
  136.         int tmp= Math.min(textR.width(),xa);
  137.         textR.offset(-tmp,0);
  138.         }
  139.     else
  140.         if(textR.lX<boundR.lX)
  141.         {
  142.                 //do a horizontal right shift
  143.             int tmp= Math.min(textR.width(),xb);
  144.             textR.offset(tmp,0);
  145.         }
  146.     /* vertical : (Has bugs! --OK for now....)*/
  147.     if(textR.tY<boundR.tY)
  148.         {
  149.                 //do a vertical down shift
  150.         int tmp= Math.min(textR.height(),ya);
  151.         textR.offset(0,tmp);
  152.         }
  153.     else
  154.         if(textR.bY>boundR.bY)
  155.         {
  156.             
  157.                 //do a vertical down shift
  158.             int tmp= Math.min(textR.height(),yb);
  159.             textR.offset(0,-tmp);
  160.         }
  161.  
  162.     };
  163.     
  164. public void Draw(Graphics g)
  165.     {
  166.     g.setFont(font);
  167.     g.setColor(Color.lightGray);
  168.     g.fill3DRect(textR.lX, textR.tY, textR.width(), textR.height(),true);
  169.     g.setColor(Color.black);
  170.     int y=textR.tY;
  171.     for (Enumeration e = xfrm.strvec.elements(); e.hasMoreElements();)
  172.         {
  173.         y+=lineHi;
  174.         g.drawString((String) e.nextElement() , textR.lX+charWid, y);
  175.                    
  176.         }; 
  177.  
  178.     };
  179.   
  180. public class Zine extends java.applet.Applet {
  181.         
  182. protected String dana = null;
  183. protected String imgna = null;
  184. protected String shot=null;
  185. protected Image  daimg;
  186.     Rect clipR;
  187.     Vector framevect;
  188.     boolean showhot=false;
  189.     
  190.     zineFrame cur_znfrm=null;
  191.     zinePopUp pop=null;
  192.     Font font;
  193. public void init() {
  194.     font = new java.awt.Font("TimesRoman", Font.PLAIN, 12);
  195.     framevect= new Vector(); 
  196.     //resize(200, 200);
  197.     dana = getParameter("dataurl");
  198.     getFrames();
  199.  
  200.     imgna = getParameter("imgurl");
  201.     daimg= getImage(getCodeBase(), imgna);
  202.     //if(daimg!=null)
  203.     //resize(daimg.getWidth(this),daimg.getHeight(this));
  204.     //else
  205.     //resize(10, 10);
  206.     shot = getParameter("showhot");
  207.  
  208.     if(shot!=null)
  209.     if(shot.equals("yes"))
  210.         showhot=true;
  211. }
  212.     
  213. public void paint(Graphics g) {
  214.     g.drawImage(daimg, 0, 0, this);    
  215.             
  216.     if(showhot) {
  217.     for ( Enumeration ee = framevect.elements(); ee.hasMoreElements();) {
  218.         zineFrame fr= (zineFrame)ee.nextElement();
  219.         g.draw3DRect(fr.r.lX, fr.r.tY, fr.r.width(), fr.r.height(),true);
  220.     }
  221.     }
  222.         
  223.     if(pop!=null) {
  224.     pop.Draw(g);    
  225.     }
  226.     g.draw3DRect(0, 0, size().width-1, size().height-1, true);
  227.  
  228. }   
  229.     /* end paint */
  230.  
  231. public void update(Graphics g) {
  232.     // Clip to the affected area
  233.     //make sure not outside applet: 
  234.     //(needed else it can paint outside applet --java bug ?)
  235.     //if (clipR != null) {
  236.     //g.clipRect(clipR.lX, clipR.tY, clipR.width(), clipR.height());
  237.     //}
  238.     paint(g);
  239. }
  240. public boolean keyDown(java.awt.Event evt, int k)
  241.     {
  242.     System.out.println("key");
  243.                  
  244.     if(k=='s'||k=='S')
  245.         {
  246.         showhot=!showhot;
  247.         clipR.lX=0;
  248.         clipR.rX=size().width;
  249.         clipR.tY=0;
  250.         clipR.bY=size().height;
  251.         repaint();
  252.         }
  253.     return true;
  254.     }
  255. public boolean mouseEnter(java.awt.Event evt)
  256.     { requestFocus();
  257.       return true;
  258.     }
  259.  
  260. public boolean mouseExit(java.awt.Event evt)
  261.     { 
  262.     if(pop!=null)
  263.         {
  264.         cur_znfrm=null;
  265.         clipR=pop.clipR;
  266.         pop=null;
  267.         repaint();
  268.         };
  269.     return true;
  270.                  
  271.     }
  272. public boolean mouseMove(java.awt.Event evt, int x, int y)
  273.     {
  274.                     
  275.     boolean change=false;
  276.     if(cur_znfrm==null)
  277.         {
  278.         for ( Enumeration ee = framevect.elements(); ee.hasMoreElements();)
  279.             {
  280.             zineFrame fr= (zineFrame)ee.nextElement();
  281.             if(fr.r.isPointInRect(x,y))
  282.                 { change=true;
  283.                 cur_znfrm=fr;
  284.                 pop = new zinePopUp(font, getFontMetrics(font));
  285.                 Rect r=new Rect(0,0,size().width,size().height);
  286.                 pop.doLayout(r,cur_znfrm);
  287.                 clipR=pop.clipR;
  288.                 };
  289.             };
  290.                         
  291.         }
  292.     else
  293.         {
  294.         if(!cur_znfrm.r.isPointInRect(x,y))
  295.             { 
  296.             cur_znfrm=null;
  297.             clipR=pop.clipR;
  298.             pop=null;
  299.             change=true;
  300.             };
  301.         };
  302.                      
  303.  
  304.     if(change)
  305.         {
  306.  
  307.         repaint();
  308.  
  309.  
  310.         };
  311.     return true;
  312.     }  
  313.      
  314.      
  315.      
  316.      
  317. protected void getFrames()
  318.     {
  319.     zineFrame zfrm=null;
  320.                 //System.out.println("zine get frames");
  321.                          
  322.     InputStream is=null;
  323.  
  324.  
  325.     try {
  326.         try {
  327.         is = new URL(getDocumentBase(), dana).openStream();
  328.         StreamTokenizer st = new StreamTokenizer(is);
  329.         st.eolIsSignificant(false);
  330.         st.commentChar('#');
  331.                                 
  332.                                  
  333.                                  
  334.                 /* parse file --NOT very clean can be improved alot */    
  335.         int i=0;
  336.         while (st.ttype != StreamTokenizer.TT_EOF)
  337.             {
  338.             st.nextToken();
  339.                                     
  340.                 /* got a new frame: */
  341.             if(st.ttype == '{')
  342.                 { 
  343.                                      
  344.                 zfrm= new zineFrame(); 
  345.                 i=0;
  346.                 continue;
  347.                 };
  348.                                      
  349.             if(st.ttype == '}')
  350.                 {framevect.addElement(zfrm);
  351.                 continue;
  352.                 }
  353.  
  354.             if(st.ttype==st.TT_NUMBER)
  355.                 {
  356.                 int  n = (int) st.nval;
  357.                 switch (i)
  358.                     {
  359.                     case 0:
  360.                     zfrm.p.x=n;
  361.                     break;
  362.                     case 1:
  363.                     zfrm.p.y=n;
  364.                     break;
  365.                     case 2:
  366.                     zfrm.r.lX=n;
  367.                     break;
  368.                     case 3:
  369.                     zfrm.r.tY=n;
  370.                     break;
  371.                     case 4:
  372.                     zfrm.r.rX=n;
  373.                     break;
  374.                     case 5:
  375.                     zfrm.r.bY=n;
  376.                     break;
  377.  
  378.                                      
  379.                     };     
  380.  
  381.                 i++;
  382.                 continue;
  383.                 };
  384.  
  385.             if(st.ttype=='"')
  386.                 { String tmp= st.sval;
  387.                 zfrm.strvec.addElement(tmp);
  388.                 continue;
  389.                 };
  390.  
  391.             };    /* while */
  392.         } finally {
  393.         if (is != null) {
  394.             is.close();
  395.         }
  396.         }
  397.  
  398.     } catch (MalformedURLException e) {
  399.     } catch (IOException e) {
  400.     }
  401.  
  402.     };     /* end getFrames()    */
  403.  
  404.  
  405. }   /* end zine class */ 
  406.    
  407.